<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchFormData" class="demo-form-inline">
      <el-form-item label="角色码">
        <el-input v-model="searchFormData.roleCode" :clearable="true" placeholder="输入角色码" style="width: 250px;" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="loadTable">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary"  icon="Plus" style="margin-bottom: 10px;" @click="onAdd">新增角色</el-button>
    <el-table
      :data="pageInfo.rows"
      border
      highlight-current-row
    >
      <el-table-column
        prop="roleCode"
        label="角色码"
        width="200"
      />
      <el-table-column
        prop="description"
        label="角色描述"
        width="200"
      />
      <el-table-column
        prop="gmtCreate"
        label="添加时间"
        width="160"
      />
      <el-table-column
        prop="gmtModified"
        label="修改时间"
        width="160"
      />
      <el-table-column
        label="操作"
        width="150"
      >
        <template #default="scope">
          <el-button type="link" text  @click="onTableUpdate(scope.row)">修改</el-button>
          <el-button type="link" text  @click="onTableDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="margin-top: 5px"
      :current-page="searchFormData.pageIndex"
      :page-size="searchFormData.pageSize"
      :page-sizes="[5, 10, 20, 40]"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="onSizeChange"
      @current-change="onPageIndexChange"
    />
    <!--dialog-->
    <el-dialog
      :title="roleDialogTitle"
      v-model="roleDialogVisible"
      :close-on-click-modal="false"
      @close="resetForm(roleForm)"
      top="100px"

    >
      <el-form
        ref="roleForm"
        :rules="roleDialogFormRules"
        :model="roleDialogFormData"
        label-width="120px"
      >
        <el-form-item prop="roleCode" label="角色码">
          <el-input v-show="roleDialogFormData.id === 0" v-model="roleDialogFormData.roleCode" />
          <span v-show="roleDialogFormData.id > 0">{{ roleDialogFormData.roleCode }}</span>
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="roleDialogFormData.description" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="roleDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onRoleDialogSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue'
import {useRoute,useRouter} from 'vue-router'
import request from '@/utils/request'
import {useMessage, useMessageBox} from '@/hooks/message'
import { Plus,Delete, Edit,Search } from '@element-plus/icons-vue'

const searchFormData=reactive({
    roleCode: '',
    pageIndex: 1,
    pageSize: 10
  })
const pageInfo=reactive({
    rows: [],
    total: 0
  })
const roleDialogVisible=ref(false)
const roleDialogTitle=ref('')
const roleForm=ref()
const roleDialogFormData=reactive({
    id: 0,
    roleCode: '',
    description: ''
  })
const roleDialogFormRules=reactive({
    roleCode: [
      { required: true, message: '不能为空', trigger: 'blur' },
      { min: 1, max: 64, message: '长度在 1 到 64 个字符', trigger: 'blur' }
    ],
    description: [
      { max: 64, message: '不能超过 64 个字符', trigger: 'blur' }
    ]
  })
const loadTable=async ()=>{
    const resp=await request('/api/role/page', searchFormData)
    Object.assign(pageInfo,resp.data)
  }
const onTableUpdate=async(row)=>{
    roleDialogTitle.value = '修改角色'
    roleDialogVisible.value = true
    await nextTick()
    Object.assign(roleDialogFormData, row)
  }
const onTableDelete=(row)=>{
    useMessageBox().confirm(`确认要删除角色【${row.roleCode}】吗？`).then(async()=>{
      await request('/api/role/del', {
        id: row.id
      })
      useMessage().tip('删除成功')
      loadTable()
    })
  }
const onRoleDialogSave=function() {
  roleForm.value.validate(async(valid) => {
      if (valid) {
        const uri = roleDialogFormData.id ? 'role.update' : 'role.add'
        await request(uri, roleDialogFormData, function() {
          roleDialogVisible.value = false
          loadTable()
        })
      }
    })
  }
const onSizeChange=function(size) {
    searchFormData.pageSize = size
    loadTable()
  }
const onAdd=function() {
    console.log(1222);
    roleDialogTitle.value = '新增角色'
    roleDialogVisible.value = true
    roleDialogFormData.id = 0
  }
const onPageIndexChange=function(pageIndex) {
  searchFormData.pageIndex = pageIndex
  loadTable()
}
const resetForm=(formEl)=>{
  console.log(formEl);
  if(!formEl){
    return ;
  }
  formEl.resetFields();
}

onMounted(()=>{
  loadTable()
})
</script>
